#include <iostream>
#include <string>

#include <cstdio>
#include <cstring>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>

struct thread_data
{
    struct sockaddr_in server;
    int sockfd;
};
void* recv_message(void* args)
{
    const thread_data* td = static_cast<const thread_data*>(args);
    while (true)
    {
        char buffer[1024];
        struct sockaddr_in tmp;
        socklen_t len = sizeof(tmp);
        ssize_t n = recvfrom(td->sockfd, buffer, 1023, 0, (struct sockaddr*)&tmp, &len);
        if (n < 0)
        {
            fprintf(stderr, "recvfrom error, errno: %d, err string: %s", errno, strerror(errno));
            continue;
        }
        buffer[n] = 0;
        std::cerr << buffer << std::endl;
    }
    return nullptr;
}
void* send_message(void* args)
{
    const thread_data* td = static_cast<const thread_data*>(args);
    std::string message = inet_ntoa(td->server.sin_addr);
    message += " iscomming...";
    sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&(td->server), sizeof(td->server));
    while (true)
    {
        std::cout << "Please Enter:";
        getline(std::cin, message);
        sendto(td->sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&(td->server), sizeof(td->server));
    }
    return nullptr;
}

void Usage(const std::string& proc)
{
    std::cout << "\n\rUsage: " << proc << " serverip serverport\n" << std::endl;
}
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    struct thread_data td;
    //创建套接字
    td.sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (td.sockfd < 0)
    {
        std::cout << "socket create error" << std::endl;
        exit(0);
    }
    //配置服务器端的套接字  // struct sockaddr_in server;
    memset(&td.server, 0, sizeof(td.server));
    td.server.sin_family = AF_INET;
    td.server.sin_port = htons(serverport);
    td.server.sin_addr.s_addr = inet_addr(serverip.c_str());
    socklen_t len = sizeof(td.server);

    pthread_t recvr, sender;
    pthread_create(&recvr, 0, recv_message, &td);
    pthread_create(&sender, 0, send_message, &td);
    
    pthread_join(recvr, nullptr);
    pthread_join(sender, nullptr);
    
    // std::string message;
    // char buffer[1024];
    // while (true)
    // {
    //     std::cout << "Please Enter:";
    //     getline(std::cin, message);
    //     sendto(td.sockfd, message.c_str(), message.size(), 0, (struct sockaddr*)&(td.server), len);

    //     struct sockaddr_in tmp;     //不使用,仅作为recvform参数传递
    //     socklen_t tmplen = sizeof(tmp);
    //     ssize_t n = recvfrom(td.sockfd, buffer, 1023, 0, (struct sockaddr*)&tmp, &tmplen);
    //     if (n < 0)
    //     {
    //         printf("recvfrom error, errno: %d, err string: %s", errno, strerror(errno));
    //         continue;
    //     }
    //     buffer[n] = 0;
    //     std::cout << buffer << std::endl;
    // }

    close(td.sockfd);
    return 0;
}